﻿using System.IO;
using Microsoft.Office.Interop.Excel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System;
using Export2Excel;
using System.Collections.Concurrent;

namespace Export2ExcelTest
{
    public class Temp
    {
        public static void RunTest()
        {
            List<LevelModelBase> modelCollection = new List<LevelModelBase>();
            List<FixedAssetInfo> result = new List<FixedAssetInfo>();

            ExcelImporter importer = new ExcelImporter(@"D:\work\Temp\软件基础表.xlsx");
            importer.DataImport<FixedAssetInfo>
                (
                    "固定资产目录",
                    3,
                    () => { return new FixedAssetInfo(); },
                    model =>
                    {
                        result.Add(model);
                        modelCollection.Add(new FixedAssetLevelPresentation(model));
                    }
                );

            FixedAssetLevelPresentation.AnalyseLevelInfo(null, modelCollection, 5);
            modelCollection.Clear();

            bool validate1 = result.First(p => p.Id == result.First(s => s.Name == "四块分离器").ParentId).Types == "（1）卸煤机械";
            bool validate2 = result.First(p => p.Id == result.First(s => s.Name == "输煤机械").ParentId).Types == "    1．输煤设备";
            bool validate3 = result.First(p => p.Id == result.First(s => s.Name == "煤粉设备").ParentId).Types == "（一）发电及供热设备";

            bool validate4 = result.First(p => p.Id == result.First(s => s.Name == "锅炉辅机").ParentId).Types == "4．锅炉设备";
            bool validate5 = result.First(p => p.Id == result.First(s => s.Name == "衡温衡湿机").ParentId).Types == "（十）生产管理用工器具";
        }

        //public static void RunTest()
        //{
        //    Microsoft.Office.Interop.Excel.Application aClass = new Microsoft.Office.Interop.Excel.Application();
        //    var workBook = aClass.Workbooks.Open(@"E:\personal\mycode\MySampleAndTest\MySampleAndTest\Export2Excel\Template\Template.xlsx");
        //    dynamic sheet1 = workBook.Sheets["sheet1"];

        //}
        public static void RunTest1()
        {
            IEnumerable<ExcelPropertyLocator> mapperCollection = new List<ExcelPropertyLocator>();
            List<LevelModelBase> modelCollection = new List<LevelModelBase>();
            List<FixedAssetInfo> result = new List<FixedAssetInfo>();

            XSSFWorkbook templateBook = InitializeWorkbook(@"D:\work\Temp\软件基础表.xlsx");
            var sheet = templateBook.GetSheetAt(2);

            foreach (IRow row in sheet)
            {
                //invalid row
                if (row.RowNum < 3)
                    continue;

                //title row
                if (row.RowNum == 3)
                {
                    //step1 : get header cells
                    var cells = row.Cells.Where(cell => !string.IsNullOrEmpty(cell.StringCellValue));

                    //step2 : get mappers
                    IEnumerable<Tuple<PropertyInfo, ColumnNameAttribute>> materials = typeof(FixedAssetInfo).GetProperties()
                        .Where
                        (
                            p => p.GetCustomAttributes(typeof(ColumnNameAttribute), false).FirstOrDefault() != null
                        )
                        .Select
                        (
                            p => new Tuple<PropertyInfo, ColumnNameAttribute>(
                            item1: p,
                            item2: (ColumnNameAttribute)p.GetCustomAttributes(typeof(ColumnNameAttribute), false).First()
                            )
                        );

                    IEnumerable<ICell> availableCells = cells.Where(cell => materials.FirstOrDefault(a => a.Item2.ColumnName == cell.StringCellValue) != null);

                    mapperCollection = availableCells.Select(cell => new ExcelPropertyLocator
                    {
                        Key = cell.ColumnIndex,
                        PropertyInfo = materials.First(p => p.Item2.ColumnName == cell.StringCellValue).Item1
                    });

                    continue;
                    //step3 : generate converter
                    //ignore
                }

                //data row
                //ignore

                //step4 : convert row to model instance
                FixedAssetInfo fixedAssetInfo = new FixedAssetInfo();
                foreach (var mapper in mapperCollection)
                {
                    mapper.SetValue(row, fixedAssetInfo);
                }

                result.Add(fixedAssetInfo);
                modelCollection.Add(new FixedAssetLevelPresentation(fixedAssetInfo));
            }

            //step5 : analyse the level 
            FixedAssetLevelPresentation.AnalyseLevelInfo(null, modelCollection, 5);

            //step6: persistence

            //reult validation
            bool validate1 = result.First(p => p.Id == result.First(s => s.Name == "四块分离器").ParentId).Types == "（1）卸煤机械";
            bool validate2 = result.First(p => p.Id == result.First(s => s.Name == "输煤机械").ParentId).Types == "    1．输煤设备";
            bool validate3 = result.First(p => p.Id == result.First(s => s.Name == "煤粉设备").ParentId).Types == "（一）发电及供热设备";

            bool validate4 = result.First(p => p.Id == result.First(s => s.Name == "锅炉辅机").ParentId).Types == "4．锅炉设备";
            bool validate5 = result.First(p => p.Id == result.First(s => s.Name == "衡温衡湿机").ParentId).Types == "（十）生产管理用工器具";

        }

        static XSSFWorkbook InitializeWorkbook(string path)
        {
            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                return new XSSFWorkbook(file);
            }
        }
    }
}
